﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class MemberGroup
    {
        public int Add(KeRui.Model.MemberGroup model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@GroupName", SqlDbType.NVarChar, 50), new SqlParameter("@GroupLevel", SqlDbType.Int, 4), new SqlParameter("@GroupColor", SqlDbType.VarChar, 10), new SqlParameter("@EPStart", SqlDbType.Int, 4), new SqlParameter("@EPEnd", SqlDbType.Int, 4), new SqlParameter("@PublishNum", SqlDbType.Int, 4), new SqlParameter("@EffectiveDate", SqlDbType.Int, 4) };
            parameters[0].Value = model.GroupName;
            parameters[1].Value = model.GroupLevel;
            parameters[2].Value = model.GroupColor;
            parameters[3].Value = model.EPStart;
            parameters[4].Value = model.EPEnd;
            parameters[5].Value = model.PublishNum;
            parameters[6].Value = model.EffectiveDate;
            return DbHelperSQL.RunProcedure("MemberGroup_ADD", parameters, out num);
        }

        public int Delete(int GroupID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@GroupID", SqlDbType.Int, 4) };
            parameters[0].Value = GroupID;
            return DbHelperSQL.RunProcedure("MemberGroup_Delete", parameters, out num);
        }

        public KeRui.Model.MemberGroup GetModel(int GroupID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@GroupID", SqlDbType.Int, 4) };
            parameters[0].Value = GroupID;
            KeRui.Model.MemberGroup group = new KeRui.Model.MemberGroup();
            DataSet set = DbHelperSQL.RunProcedure("MemberGroup_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["GroupID"].ToString() != "")
                {
                    group.GroupID = int.Parse(set.Tables[0].Rows[0]["GroupID"].ToString());
                }
                group.GroupName = set.Tables[0].Rows[0]["GroupName"].ToString();
                if (set.Tables[0].Rows[0]["GroupLevel"].ToString() != "")
                {
                    group.GroupLevel = int.Parse(set.Tables[0].Rows[0]["GroupLevel"].ToString());
                }
                group.GroupColor = set.Tables[0].Rows[0]["GroupColor"].ToString();
                if (set.Tables[0].Rows[0]["EPStart"].ToString() != "")
                {
                    group.EPStart = int.Parse(set.Tables[0].Rows[0]["EPStart"].ToString());
                }
                if (set.Tables[0].Rows[0]["EPEnd"].ToString() != "")
                {
                    group.EPEnd = int.Parse(set.Tables[0].Rows[0]["EPEnd"].ToString());
                }
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    group.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                if (set.Tables[0].Rows[0]["UpdateTime"].ToString() != "")
                {
                    group.UpdateTime = DateTime.Parse(set.Tables[0].Rows[0]["UpdateTime"].ToString());
                }
                if (set.Tables[0].Rows[0]["PublishNum"].ToString() != "")
                {
                    group.PublishNum = int.Parse(set.Tables[0].Rows[0]["PublishNum"].ToString());
                }
                if (set.Tables[0].Rows[0]["EffectiveDate"].ToString() != "")
                {
                    group.EffectiveDate = int.Parse(set.Tables[0].Rows[0]["EffectiveDate"].ToString());
                }
                return group;
            }
            return null;
        }

        public int Update(KeRui.Model.MemberGroup model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@GroupID", SqlDbType.Int, 4), new SqlParameter("@GroupName", SqlDbType.NVarChar, 50), new SqlParameter("@GroupLevel", SqlDbType.Int, 4), new SqlParameter("@GroupColor", SqlDbType.VarChar, 10), new SqlParameter("@EPStart", SqlDbType.Int, 4), new SqlParameter("@EPEnd", SqlDbType.Int, 4), new SqlParameter("@PublishNum", SqlDbType.Int, 4), new SqlParameter("@EffectiveDate", SqlDbType.Int, 4) };
            parameters[0].Value = model.GroupID;
            parameters[1].Value = model.GroupName;
            parameters[2].Value = model.GroupLevel;
            parameters[3].Value = model.GroupColor;
            parameters[4].Value = model.EPStart;
            parameters[5].Value = model.EPEnd;
            parameters[6].Value = model.PublishNum;
            parameters[7].Value = model.EffectiveDate;
            return DbHelperSQL.RunProcedure("MemberGroup_Update", parameters, out num);
        }
    }
}

